﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Windows.Controls;

namespace LaunchMeNot
{
    /// <summary>
    /// Thanks to Samuel Moura - http://sweux.com/blogs/smoura/index.php/wpf/2009/05/19/wpf-toolkit-datagrid-part-iii-playing-with-datagridcolumns-and-datagridcells/
    /// </summary>
    public class AutoCommitCheckBoxColumn : DataGridCheckBoxColumn
    {
        private void checkBox_Unchecked(object sender, RoutedEventArgs e)
        {
            CommitCellEdit((FrameworkElement)sender);
        }

        private void checkBox_Checked(object sender, RoutedEventArgs e)
        {
            CommitCellEdit((FrameworkElement)sender);
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            var checkBox = (CheckBox)base.GenerateEditingElement(cell, dataItem);

            checkBox.Checked += checkBox_Checked;
            checkBox.Unchecked += checkBox_Unchecked;

            return checkBox;
        }
    }
}